package com.woniuxy.woniuhis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.woniuhis.entity.Repository;
import com.woniuxy.woniuhis.exception.MedicineServiceException;
import com.woniuxy.woniuhis.service.RepositoryService;
import com.woniuxy.woniuhis.util.ResponseInfo;

@Controller
public class RepositoryController {

	@Autowired
	RepositoryService repositoryService;
	
	
	/**
     * @param req
     * @return
     * 跳转到调拨药物页面的controller方法
     */
    @RequestMapping("toAllotMedicine")
    @ResponseBody
    public ModelAndView toAllotMedicineController(HttpServletRequest req,HttpServletResponse resp
    		,Model model) {
    	
    	System.out.println("toAllotMedicineController");
    	
    	String mediId= req.getParameter("mediId");
    	System.out.println(mediId);
    	String mediName = req.getParameter("mediName");
    	System.out.println(mediName);
    	String pharId = req.getParameter("pharId");
    	System.out.println(pharId);
    	String stock = req.getParameter("stock");
    	System.out.println(stock);

    	
    	ModelAndView mv = new ModelAndView();
		
    	try {
    		
        	Repository repository = new Repository();
        	
        	if (mediId!=null&&mediId.length()>0) {
        		repository.setMediId(mediId);
			}else {
				mediId="未设定";
				repository.setMediId(mediId);
			}
        	
        	if (mediName!=null&&mediName.length()>0) {
        		repository.setMediName(mediName);
			}else {
				mediName="未设定";
				repository.setMediName(mediName);
			}
        	
        	if (pharId!=null&&pharId.length()>0) {
        		repository.setPharId(pharId);
			}else {
				pharId="未设定";
				repository.setPharId(pharId);
			}
        	
        	
        	repository.setStock(Integer.parseInt(stock));

        	
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setData(repository);
        	responseInfo.setMsg("跳转页面成功");
        	responseInfo.setState(200);
        	
        	System.out.println("跳转");
        	
        	//mv.setViewName("views/pharmacy/medimanage.html");
        	mv.setViewName("views/pharmacy/mediallot.html");
        	mv.addObject("info",responseInfo);
        	
        	return mv;
		} catch (MedicineServiceException e) {
			System.out.println("toAllotMedicineController错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("toAllotMedicineController服务方法错误");
        	responseInfo.setState(500);
        	
        	mv.setViewName("views/pharmacy/mediallot.html");
        	mv.addObject("info",responseInfo);
        	
        	return mv;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("其他错误");
        	responseInfo.setState(500);
        	
        	mv.setViewName("views/pharmacy/mediallot.html");
        	mv.addObject("info",responseInfo);
        	
        	return mv;
		}

}	
		
    /**
     * @param req
     * @return
     * 通过药物名字来查看库存
     */
    @RequestMapping("getRepository")
    @ResponseBody
	public ResponseInfo findRepositoryByMediName(HttpServletRequest req) {
    	

    	System.out.println("findRepositoryByMediName");
    	String name = req.getParameter("mediname");
    	System.out.println(name);
    	
    	ModelAndView mv=new ModelAndView();
    	
    	try{
    		
        	Repository repository = new Repository();
        			
        	
        	
        	if (name!=null&&name!=""){
        		repository.setMediName(name);
			}else {
				name="";
				repository.setMediName(name);
			}
        	

    		List<Repository> res = repositoryService.findRepositoryByMediName(repository);
        	ResponseInfo responseInfo = new ResponseInfo();
        	
        	responseInfo.setData(res);
        	responseInfo.setMsg("跳转页面成功");
        	responseInfo.setState(200);
        	
        	
        	return responseInfo;
		} catch (MedicineServiceException e) {
			System.out.println("findRepositoryByMediName错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("findRepositoryByMediName服务方法错误");
        	responseInfo.setState(500);
        	return responseInfo;
		} catch (NullPointerException e) {
			System.out.println("不完整错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("请输入全部的数据");
        	responseInfo.setState(500);
        	return responseInfo;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("其他错误");
        	responseInfo.setState(500);
        	return responseInfo;
		}

	
    }
	
}
